package com.zxl.live.screen.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.ads.MobileAds;
import com.zxl.live.screen.R;
import com.zxl.live.screen.ads.banner.AdmobBannerProxy;
import com.zxl.live.screen.ui.fragment.CallThemeFragment;
import com.zxl.live.screen.ui.fragment.ScreenFragment;
import com.zxl.live.screen.ui.helper.GuideController;
import com.zxl.live.tools.analytics.Event;
import com.zxl.live.tools.base.BaseAppCompatActivity;
import com.zxl.live.tools.common.ApplicationHelper;

public class MainActivity extends BaseAppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private boolean mClickOnce;

    private Fragment mCurrent;

    private DrawerLayout mDrawer;

    private NavigationView mNavigationView;

    private GuideController mGuideController;

    public static AdmobBannerProxy mBannerProxy = new AdmobBannerProxy(Event.ADS_SCREEN_SETTINGS);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MobileAds.initialize(this, "ca-app-pub-1417586053260517~7148731433");
        setMainActivity(true);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        mDrawer.addDrawerListener(toggle);
        toggle.syncState();
        mGuideController = new GuideController(this);
        mNavigationView = (NavigationView) findViewById(R.id.nav_view);
        mNavigationView.setNavigationItemSelectedListener(this);
        if (!mGuideController.isShowGuide()) {
            mNavigationView.getMenu().findItem(R.id.nav_rating).setVisible(false);
        }
        setNavigationItem(R.id.nav_screen);

    }

    @Override
    protected void onResume() {
        super.onResume();
        mGuideController.showGuide(true);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            if (!ScreenFragment.class.isInstance(mCurrent)) {
                setNavigationItem(R.id.nav_screen);
                return;
            }

            if (mClickOnce) {
                super.onBackPressed();
            }
            Toast.makeText(this, R.string.quit_tip, Toast.LENGTH_SHORT).show();
            mClickOnce = true;
            ApplicationHelper.postUiThreadDelayed(new Runnable() {
                @Override
                public void run() {
                    mClickOnce = false;
                }
            }, 2000);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBannerProxy.recycle();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        refreshStatus(item.getItemId());
        return true;
    }

    private void setNavigationItem(int itemId) {
        mNavigationView.setCheckedItem(itemId);
        refreshStatus(itemId);
    }

    private void refreshStatus(int itemId) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        switch (itemId) {
            case R.id.nav_screen:
                if (!ScreenFragment.class.isInstance(mCurrent)) {
                    mCurrent = new ScreenFragment();
                    setTitle(R.string.setting_title);
                    fragmentManager.beginTransaction().replace(R.id.content, mCurrent).setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out).commit();
                }
                break;
            case R.id.nav_call:
                if (!CallThemeFragment.class.isInstance(mCurrent)) {
                    saveScreenFragmentData();
                    mCurrent = new CallThemeFragment();
                    setTitle(R.string.call_title);
                    fragmentManager.beginTransaction().replace(R.id.content, mCurrent).setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out).commit();
                }
                break;
            case R.id.nav_feedback:
                startActivity(new Intent(this, FeedbackActivity.class));
                break;
            case R.id.nav_about:
                startActivity(new Intent(this, AboutActivity.class));
                break;
            case R.id.nav_rating:
                mGuideController.showGuide(false);
                break;
        }
        mDrawer.closeDrawer(GravityCompat.START);
    }

    private void saveScreenFragmentData() {
        if (ScreenFragment.class.isInstance(mCurrent)) {
            ScreenFragment screenFragment = (ScreenFragment) mCurrent;
            getIntent().putExtra("position", screenFragment.getCurrentTab());
        }
    }
}
